package tech.aistar.corejava.day17.day17home;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * Created by 豆梦寒 on 2019/8/7.
 */
public class TestEmp {
    public static void main(String[] args) {
        IEmpDao empdao = new IEmpDaoImpl();
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("欢迎来到员工信息页面!");
            System.out.println("录入员工信息请输1");
            System.out.println("查询员工信息请输2");
            System.out.println("退出页面请输3");
            int s =Integer.parseInt(sc.nextLine());
            String s1 = null;
            switch(s){
                case 1:
                    Emp emp =new Emp();
                    System.out.println("请输入员工id");
                    int id = Integer.parseInt(sc.nextLine());
                    if (Sid(empdao,id)){
                        emp.setEmpId(id) ;
                    }else {
                        System.out.println("员工编号重复,请重新输入!");
                        break;
                    }

                    System.out.println("请输入员工姓名");
                    emp.setEmpName(sc.nextLine());
                    System.out.println("请输入员工生日");
                    s1 = sc.nextLine();
                    emp.setEmpBir(getDate(s1));
                    System.out.println("请输入员工工种");
                    emp.setEmpJob(sc.nextLine());
                    empdao.save(emp);
                    break;
                case 2:
                    List<Emp> list = empdao.findAll();
                    if (list.size()==0){
                        System.out.println("对不起,没有数据!");
                    }else{
                        Collections.sort(list, new Comparator<Emp>() {
                            @Override
                            public int compare(Emp o1, Emp o2) {
                                if(o1.getEmpBir().after(o2.getEmpBir())){
                                    return -1;
                                }else if (o1.getEmpBir().before(o2.getEmpBir())){
                                    return 1;
                                }
                                return 0;
                            }
                        });
                        Iterator<Emp> iter = list.iterator();
                        while(iter.hasNext()){
                            Emp emp1 = iter.next();
                            System.out.println(emp1.getEmpId()+"\t"+emp1.getEmpName()+"\t"+outDate(emp1.getEmpBir())+"\t"+emp1.getEmpJob());
                        }
                    }

                    break;
                default:
                    break;
            }
            if (s == 3){
                PrintWriter pw = null;

                try {
                    pw =new PrintWriter(new FileWriter("src/tech/aistar/corejava/day17home/emp.txt",false),true);

                    List<Emp> list = empdao.findAll();
                    Iterator<Emp> iter = list.iterator();
                    while(iter.hasNext()){
                        Emp emp1 = iter.next();
                        pw.write("|"+emp1.getEmpId()+"|"+ emp1.getEmpName()+"|"+outDate(emp1.getEmpBir())+"|"+emp1.getEmpJob()+"\n");
                    }
                    pw.flush();

                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    pw.close();
                }
                break;
            }
        }
    }

    public static Date getDate(String s){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    public static String outDate(Date date){
        String datdString =   date.toString();
//        System.out.println(datdString);
        //将字符串转化为date类型，格式2016-10-12
        SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
        Date dateTrans = null;
        try {
            dateTrans = format.parse(datdString);
            return new SimpleDateFormat("yyyy-MM-dd").format(dateTrans);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return datdString;
    }

    public static boolean Sid(IEmpDao empdao,int id) {
        List<Emp> list = empdao.findAll();
        for (int i = 0; i < list.size(); i++) {
            String s = String.valueOf(list.get(i));
            if (s.split("\\|")[1].equals(String.valueOf(id))) {
                return false;
            }
        }

        return true;
    }
}
